package easymusic.baseutility;

import java.lang.reflect.Constructor;
import java.util.HashMap;
import easymusic.baseinterface.*;
import android.app.Service;
import android.content.Context;

abstract class AbstractService implements IBasicService{
	protected static HashMap<ServiceID, IBasicService> m_mapService = new HashMap<ServiceID, IBasicService>();
	protected static class ServiceReflector{
		public ServiceID m_serviceID;
		public String m_strClassName;
		public ServiceReflector(ServiceID id, String strClassName){
			m_serviceID = id;
			m_strClassName = strClassName;
		}
	}
	// probably some day it must read from XML for getting the service name
	protected static ServiceReflector[] m_arrayService = {
			new ServiceReflector(ServiceID.ManagerService, "easymusic.baseutility.EasyMusicManagerService"),
			new ServiceReflector(ServiceID.AppDataService, "easymusic.baseutility.EasyMusicAppDataService"),
			new ServiceReflector(ServiceID.MediaService, "easymusic.baseutility.EasyMusicMediaService"),
			new ServiceReflector(ServiceID.MediaDataService, "easymusic.baseutility.EasyMusicMediaDataService"),
	};
	static{
		for (ServiceReflector s: m_arrayService){
			try {
				Class<?> someClass = Class.forName(s.m_strClassName);
				Constructor<?> someConstructor = someClass.getConstructor();
				IBasicService service = (IBasicService)someConstructor.newInstance();
				if (null == service){
					throw new Exception("please make sure every service class inherits from IBasicService");
				}
				m_mapService.put(s.m_serviceID, service);
			} catch (Exception e) {
				e.printStackTrace();
			}	
		}
	}
	

	@Override
	public IBasicService getService(ServiceID id) {
		return m_mapService.get(id);
	}
	
	// here is the only way to get service manager object
	public static IBasicService getServiceManager(){
		IBasicService pService = m_mapService.get(ServiceID.ManagerService);		
		return pService;
	}
}
